<?php

/**
 * Description of formContent
 *
 * @author Kam
 */
class My_View_Helper_FormWyswig extends Zend_View_Helper_FormTextarea {

    public function formWyswig($name, $value = null, $attribs = null) {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/libs/wysihtml5/bootstrap-wysihtml5.css');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/libs/wysihtml5/stylesheet.css');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/libs/wysihtml5/stylesheet.css');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/libs/wysihtml5/parser_rules/simple.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/libs/wysihtml5/wysihtml5-0.3.0.min.js');
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/libs/wysihtml5/bootstrap-wysihtml5.js');
        $this->view->headScript()->captureStart();
        $wid = $this->view->escape($id) . '_' . uniqid();
        $class = isset($attribs['class']) ? $attribs['class'] : '';
        $class .= ' ' . $wid;
        $attribs['class'] = $class;
        $ctrls = $attribs['controls'];
        ?>
        $(function() {
        $('.<?= $wid ?>').wysihtml5({
            parserRules: wysihtml5ParserRules,
        <?php /** / ?>
          "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true
          "emphasis": true, //Italics, bold, etc. Default true
          "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
          "html": false, //Button which allows you to edit the generated HTML. Default false
          "link": false, //Button to insert a link. Default true
          "image": false, //Button to insert an image. Default true,
          "color": false //Button to change color of font
          <?php /* */ ?>
        <?php
        $tmp = array();
        foreach ($ctrls as $key => $val) {
            $tmp[] = '"' . $key . '": ' . ($val ? 'true' : 'false');
        }
        echo implode(',' . "\n", $tmp);
        ?>
        });
        editor = $('.<?= $wid ?>').data("wysihtml5").editor;
        editor.observe("load", function() {
          editor.composer.element.addEventListener("keyup", function() {
            editor.composer.iframe.style.height = editor.composer.element.scrollHeight + "px";
          });
        });
        });
        <?php
        $this->view->headScript()->captureEnd();

        $xhtml = $this->formTextarea($name, $value, $attribs);
        return $xhtml;
    }

}
